package cn.swu.lzr.OnlineNumber;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.Writer;

public class OnlineNumberServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {

           Integer OnlineNumber = (Integer) getServletContext().getAttribute("onlineCount");

            ObjectMapper mapper = new ObjectMapper();

            String json = mapper.writeValueAsString(OnlineNumber);

            response.setContentType("application/JSON");
            response.setCharacterEncoding("UTF-8");

        try (Writer writer =  response.getWriter()){
            writer.write(json);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}
